package schedule;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import utils.DateUtil;

public class Initial extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5162985733935305559L;

	public void init() throws ServletException {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 启动计划任务");

		SchedulerFactory schedulerFactory = new StdSchedulerFactory();

		try {
			Scheduler scheduler = schedulerFactory.getScheduler();

			JobDetail mortgageSchedule = new JobDetail("mortgageSchedule", "mortgageJobGroup", MortgageSchedule.class);
			JobDetail settleSchedule = new JobDetail("settleSchedule", "settleJobGroup", SettleSchedule.class);
			JobDetail logicSchedule = new JobDetail("logicSchedule", "logicJobGroup",LogicSchedule.class);
			JobDetail websiteSchedule = new JobDetail("websiteSchedule", "websiteJobGroup", WebsiteSchedule.class);

			// Initiate CronTrigger with its name and group name
			CronTrigger mortgageTrigger = new CronTrigger("mortgageTrigger", "mortgageTrgGroup");
			CronTrigger settleTrigger = new CronTrigger("settleTrigger", "settleTrgGroup");
			CronTrigger logicTrigger = new CronTrigger("logicTrigger", "logicTrgGroup");
			CronTrigger websiteTrigger = new CronTrigger("websiteTrigger", "websiteTrgGroup");

			mortgageTrigger.setCronExpression(new CronExpression("0 00 01 * * ?"));
			settleTrigger.setCronExpression(new CronExpression("0 15 01 * * ?"));
			logicTrigger.setCronExpression(new CronExpression("0 30 01 * * ?"));
			websiteTrigger.setCronExpression(new CronExpression("0 00 02 * * ?"));
			
			// schedule a job with JobDetail and Trigger
			scheduler.scheduleJob(mortgageSchedule, mortgageTrigger);
			scheduler.scheduleJob(settleSchedule, settleTrigger);
			scheduler.scheduleJob(logicSchedule, logicTrigger);
			scheduler.scheduleJob(websiteSchedule, websiteTrigger);
			scheduler.start();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("计划任务初始化错误");
		}

	}

}
